import Constant.constant;
import utils.EmptyException;

import java.util.Arrays;
public class MyStack {
    //以顺序表方式实现栈
    public int[] elem;
    public int usedSize;

    public MyStack() {//创建栈的初始容量
        this.elem=new int[constant.CAPACITY];
    }
    public void push(int val) {//入栈操作
        //1.检查是否为满
        boolean flg=isFull();
        if(flg==true){
            Arrays.copyOf(this.elem,2*this.elem.length);
        }else{
            this.elem[usedSize++]=val;
        }
    }
    private boolean isFull(){
        return this.elem.length==usedSize;
    }

    public int pop() {//出栈操作
        //1.判断是否为空
        if(isEmpty()){
            throw new EmptyException(constant.EMPTY);
        }
        int val=this.elem[usedSize-1];
        usedSize--;
       return val;
    }
    private boolean isEmpty(){
        return this.usedSize==0;
    }
    //获取栈顶元素 但是不删除
    public int peek() {
        //1.判断是否为空
        if(isEmpty()){
            throw new EmptyException(constant.EMPTY);
        }else{
            int val=this.elem[usedSize-1];
            return val;

        }
    }
}
